package cn.iocoder.yudao.module.member.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberInformationDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;

    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        MemberUserDO user = userService.getUser(getLoginUserId());
        MemberLevelDO level = levelService.getLevel(user.getLevelId());
        return success(MemberUserConvert.INSTANCE.convert(user, level));
    }

    @PutMapping("/update")
    @Operation(summary = "修改基本信息")
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile-by-weixin")
    @Operation(summary = "基于微信小程序的授权码，修改用户手机")
    public CommonResult<Boolean> updateUserMobileByWeixin(@RequestBody @Valid AppMemberUserUpdateMobileByWeixinReqVO reqVO) {
        userService.updateUserMobileByWeixin(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码", description = "用户忘记密码时使用")
    @PermitAll
    public CommonResult<Boolean> resetUserPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }


    // ========== 提交材料 ==========
    @RequestMapping(value = "/upload_order_personnel_information_img", method = {RequestMethod.POST})
    @Operation(summary = "上传办理护照个人信息图片")
    @Parameters({@Parameter(name = "files", description = "文件附件,支持同时上传多个文件", required = true), @Parameter(name = "type", description = "文件类型  1:照片 2:护照 3:身份证国徽 4:身份证人像 5:户口本全册 6:结婚证/离婚证 7:辅助资产证明"), @Parameter(name = "tradeOrderId", description = "交易订单编号"), @Parameter(name = "fullName", description = "全名", required = true)})
    public CommonResult<Boolean> uploadPersonalInformationImg(@RequestParam(value = "files", required = false) MultipartFile[] files, @RequestParam(value = "type",required = false) Integer type, @RequestParam(value = "tradeOrderId", required = false) Long tradeOrderId, @RequestParam(value = "fullName") String fullName) throws Exception {
        return userService.uploadPersonalInformation(files, type, tradeOrderId, fullName);
    }

    @RequestMapping(value = "/upload_order_personnel_information_base", method = {RequestMethod.POST})
    @Operation(summary = "上传办理护照个人基础信息")
    public CommonResult<Boolean> personalInformationBase(@Valid @RequestBody PersonnelInfoBaseVO personnelInfoBaseVO) {
        return userService.personalInformationBase(personnelInfoBaseVO);
    }

    @RequestMapping(value = "/del_order_personnel_information", method = {RequestMethod.GET})
    @Operation(summary = "删除订单关联的个人基础信息")
    @Parameters({
            @Parameter(name = "tradeOrderId", description = "订单id",required = true),
            @Parameter(name = "fullName", description = "全名",required = true)})

    public CommonResult<Boolean> delPersonalInformation(Long tradeOrderId,String fullName) {
        return userService.delPersonalInformation(tradeOrderId,fullName);
    }

    @RequestMapping(value = "/query_order_personnel_information", method = {RequestMethod.GET})
    @Operation(summary = "查询订单关联的个人基础信息")
    @Parameters({
            @Parameter(name = "tradeOrderId", description = "订单id",required = true)})
    public CommonResult<List<MemberInformationDO>> queryPersonalInformation(Long tradeOrderId) {
        return userService.queryPersonalInformation(tradeOrderId);
    }


}